<?php

/**
 * LEAGUEMan
 * 
 * Copyright (C) 2007 Marko Bischof
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
defined('_VALID_MOS') or die('Direct Access to this location is not allowed.');

if (!$project instanceof LM_Table_Project) {
    LM::redirect("projects", "ERROR in " . __FILE__);
}

switch ($task) {

    case "matrix":
    case "matches":

        $row = LM::getLMTable("Round");
        if (!$row->load($cid[0])) {
            LM::redirect("rounds", "No such Round available!");
        }

        LM::setSessionValue("_filterresultsbyround_", $row->getID());
        LM::redirect($task);
        break;

    case 'setcurrent':

        $row = LM::getLMTable("Round");
        if (!$row->load(intval($cid[0]))) {
            LM::redirect("rounds", "No such Round available!");
        }

        if ($row->ordering) {
            $project->project_cur_round = $row->ordering;
            $project->store();
        }

        LM::redirect("rounds");

        break;
        
    case 'toggleOptionShowResults':

        $row = LM::getLMTable("Round");
        if (!$row->load(intval($cid[0]))) {
            LM::redirect("rounds", "No such Round available!");
        }

        $row->showResults = 1 - $row->showResults;
        $row->store();

        LM::redirect("rounds");

        break;

    case 'toggleOptionShowTable':

        $row = LM::getLMTable("Round");
        if (!$row->load(intval($cid[0]))) {
            LM::redirect("rounds", "No such Round available!");
        }

        $row->showTable = 1 - $row->showTable;
        $row->store();

        LM::redirect("rounds");

        break;

    case 'toggleOptionShowInfo':

        $row = LM::getLMTable("Round");
        if (!$row->load(intval($cid[0]))) {
            LM::redirect("rounds", "No such Round available!");
        }

        $row->showInfo = 1 - $row->showInfo;
        $row->store();

        LM::redirect("rounds");

        break;

    case 'create':

        $from = intval(mosGetParam($_POST, "round_name_from", 0));
        $to = intval(mosGetParam($_POST, "round_name_to", 0));
        $format = mosGetParam($_POST, "round_name_format", "%s.Spieltag");

        $sql = "SELECT ordering FROM #__leagueman_round WHERE round_project = " . $project->getID();
        $database->setQuery($sql);
        $rounds = $database->loadResultArray();

        for ($i = $from; $i <= $to; $i++) {
            if (!in_array($i, $rounds)) {
                $row = LM::getLMTable("Round");
                $row->round_name = sprintf($format, $i);
                $row->round_name_short = $i;
                $row->round_project = $project->getID();
                $row->ordering = $i;
                $row->store();
            }
        }

        LM::redirect("rounds");
        break;

    case "editarticle":

        $round = LM::getLMTable("Round");
        if (!$round->load($cid[0])) {
            LM::redirect("rounds", "No such round available");
        }

        $section_id = $project->getContentItemSection();
        $content_id = $round->content_id;

        if ($section_id && $content_id) {
            mosRedirect("index2.php?option=com_content&sectionid=$section_id&task=edit&hidemainmenu=1&id=$content_id");
        } else {
            LM::redirect("rounds", "No such Content Item available");
        }
        break;

    case "createarticle":

        $total = count($cid);

        $k = 0;
        for ($i = 0; $i < $total; $i++) {

            $round = LM::getLMTable("Round");
            if (!$round->load($cid[$i])) {
                continue;
            }

            if ($round->content_id > 0) {
                continue;
            }

            $section_cat = $project->getParam("section_category", "");
            if (empty($section_cat)) {
                LM::redirect("rounds", "No Content Section or Category defined");
            }

            $section = 0;
            $category = 0;

            list($section, $category) = explode(":", $section_cat);

            if ($section && $category) {

                $roundresult = new Roundresult();
                $roundresult->setProject($project);
                $roundresult->setRound($round);

                $template = LM::getTemplate();
                $template->assign("roundresult", $roundresult);

                $introtext = $template->fetch($project->getTemplate() . "/introtext.tpl.php");
                $fulltext = $template->fetch($project->getTemplate() . "/fulltext.tpl.php");

                $row = new mosContent($database);

                $row->title = $project->getName() . ": " . $round->getName();
                $row->title_alias = $project->getName() . ": " . $round->getName();
                $row->sectionid = $section;
                $row->catid = $category;
                $row->created = date('Y-m-d H:i:s');
                $row->created_by = $my->id;
                $row->ordering = $round->getOrdering();

                $row->introtext = trim($introtext);
                $row->fulltext = trim($fulltext);

                if (!$row->store()) {
                    //LM::redirect("rounds", "Could not store content item");
                    continue;
                } else {

                    $round->content_id = $row->id;
                    $round->store();
                    $k++;

                    //LM::redirect("rounds", "content item saved");
                }
            }
        }

        LM::redirect("rounds", "$k articles from $total created");
        break;

    case "createtopics":

        break;

    case 'new':
    case 'edit':

        $row = LM::getLMTable("Round");

        if ($task == "edit") {
            $row->load($cid[0]);
        }

        $template = LM::getTemplate();

        $template->assign("view", "admin/form_rounds.tpl.php");
        $template->assign("project", $project);
        $template->assign("team", $team);
        $template->assign("my", $my);
        $template->assign("row", $row);

        $template->display("admin/view.tpl.php");
        break;

    case 'save':
        $row = LM::getLMTable("Round");
        $row->save($_POST);
        LM::redirect("rounds");
        break;

    case 'saveorder':

        $total = count($cid);

        for ($i = 0; $i < $total; $i++) {

            $round_id = $database->Quote(intval($cid[$i]));
            $ordering = $database->Quote(intval($_POST["ordering"][$i]));

            $query = "UPDATE #__leagueman_round SET ordering = $ordering
				   	   WHERE round_id = $round_id";

            $database->setQuery($query);
            if (!$database->query()) {
                echo "<script> alert('" . $database->getErrorMsg() . "'); window.history.go(-1); </script>\n";
                exit();
            }
        }
        LM::redirect("rounds");
        break;

    case 'cancel':
        LM::redirect("rounds");
        break;

    case 'remove':

        $row = LM::getLMTable("Round");

        $total = count($cid);

        $i = 0;
        foreach ($cid AS $id) {
            $sql = "SELECT COUNT(match_id) FROM #__leagueman_match
					 WHERE match_project = " . $project->getID() . " AND match_round = " . intval($id);
            $database->setQuery($sql);
            $can_delete = $database->loadResult() == 0;

            if ($can_delete) {
                if ($row->delete($id)) {
                    $i++;
                }
            }
        }
        LM::redirect("rounds", $i . " from " . $total . " Rounds removed.");
        break;

    default:

        $template = LM::getTemplate();
        $template->assign("view", "admin/list_rounds.tpl.php");
        $template->assign('project', $project);
        $template->assign('team', $team);
        $template->assign('rows', $project->getRounds());
        $template->assign('my', $my);

        $template->display('admin/view.tpl.php');
        break;
}